package com.ken.work.mvc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.google.gson.Gson;

public abstract class MyBaseController {

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	public abstract void setHttpServlet(HttpServletRequest request,HttpServletResponse response);
	
	
	public <T> T GetParamObj(Class<T> t)
	{
		T obj = null;
		try {
			obj = t.newInstance();
			Map map = request.getParameterMap();
			BeanUtils.populate(obj, map);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		
		return obj;
		
//		T bean = beanClass.newInstance();
//        Map map = request.getParameterMap();
//        BeanUtils.populate(bean, map);
	}
	
	public String getGsonStr(Object obj) {
		Gson gson = new Gson();
		String ret = gson.toJson(obj);
		StringBuffer sbfBuffer = new StringBuffer();
		sbfBuffer.append("<html>");
		sbfBuffer.append("<body><br>");
		sbfBuffer.append(ret);
		sbfBuffer.append("<br></body>");
		sbfBuffer.append("</html>");
		return ret;
	}

}
